package cn.tedu.shoot;


import java.awt.image.BufferedImage;
import java.util.Random;

public abstract class FlyingObject {
    public static final int LIVE = 0;
    public static final int DEAD = 1;
    public static final int REMOVE = 2; //删除的

    protected int width;  //宽
    protected int height; //高
    protected int x;      //x坐标
    protected int y;      //y坐标
    protected int state = LIVE;

    public abstract void step();

    //大敌机、小敌机、小蜜蜂
    public FlyingObject(int width,int height){
        this.width = width;
        this.height = height;
        Random rand = new Random();
        x = rand.nextInt(World.WIDTH-width+1);//x:0到(窗口宽-小敌机宽)之内的随机数
        y = -height;//y:负的小敌机的高
    }

    //英雄机、子弹、天空
    public FlyingObject(int width,int height,int x,int y){
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
    }

    public boolean isLive(){
        return state == LIVE;
    }

    public boolean isDead(){
        return state == DEAD;
    }

    public boolean isRemove(){
        return state == REMOVE;
    }

    public abstract BufferedImage getImage();

    public boolean isOutOfBounds(){
        return y>=World.HEIGHT;
    }

    public boolean isHit(FlyingObject other){
        int x1 = this.x - other.width;
        int x2 = this.x + this.width;
        int y1 = this.y - other.height;
        int y2 = this.y + this.height;
        int x = other.x;
        int y = other.y;
        return x>=x1 && x<=x2 && y>=y1 && y<=y2;
    }

    public void goDead(){
        state = DEAD;
    }


}
